package com.hkbigdata.datagovernance.meta.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hkbigdata.datagovernance.meta.bean.TableMetaInfo;
import com.hkbigdata.datagovernance.meta.bean.TableMetaInfoExtra;
import com.hkbigdata.datagovernance.meta.constant.MetaConst;
import com.hkbigdata.datagovernance.meta.mapper.TableMetaInfoExtraMapper;
import com.hkbigdata.datagovernance.meta.service.TableMetaInfoExtraService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 元数据表附加信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-01-08
 */
@DS("mysqlWriting")
@Service
public class TableMetaInfoExtraServiceImpl extends ServiceImpl<TableMetaInfoExtraMapper, TableMetaInfoExtra> implements TableMetaInfoExtraService {
    @Override
    public void genExtraListByMetaList(List<TableMetaInfo> tableMetaInfoList) {
//        创建集合用于创建辅助信息对象
        ArrayList<TableMetaInfoExtra> tableMetaInfoExtras = new ArrayList<>(tableMetaInfoList.size());


//        通过表名和库名查看是否已经有辅助信息，若没有，则补充辅助信息
        for (TableMetaInfo tableMetaInfo : tableMetaInfoList) {
//            根据表名和库名查询辅助信息表中是否有对应的辅助信息
            TableMetaInfoExtra tableMetaInfoExtra = getOne(new QueryWrapper<TableMetaInfoExtra>()
                    .eq("schema_name", tableMetaInfo.getSchemaName())
                    .eq("table_name", tableMetaInfo.getTableName()));
//            判断辅助信息是否为空
            if (tableMetaInfoExtra == null) {
//                初始化辅助信息
                tableMetaInfoExtra = new TableMetaInfoExtra();

//                设置表名库名
                tableMetaInfoExtra.setTableName(tableMetaInfo.getTableName());
                tableMetaInfoExtra.setSchemaName(tableMetaInfo.getSchemaName());

//                设置存储周期类型
                tableMetaInfoExtra.setLifecycleType(MetaConst.LIFECYCLE_TYPE_UNSET);

//                设置安全级别
                tableMetaInfoExtra.setSecurityLevel(MetaConst.SECURITY_LEVEL_UNSET);

//                设置数仓级别
                tableMetaInfoExtra.setDwLevel(getInitDwLevelByTableName(tableMetaInfo.getTableName()));

//                创建时间
                tableMetaInfoExtra.setCreateTime(new Date());

//                加入列表
                tableMetaInfoExtras.add(tableMetaInfoExtra);
            }
        }

//        将辅助信息保存到mysql
        saveBatch(tableMetaInfoExtras);
    }
    private String getInitDwLevelByTableName(String tableName){
//        设置数仓层级的方法
        if(tableName.startsWith("ods")){
            return MetaConst.DW_LEVEL_ODS;
        } else if (tableName.startsWith("dwd")) {
            return MetaConst.DW_LEVEL_DWD;
        }else if (tableName.startsWith("dim")) {
            return MetaConst.DW_LEVEL_DIM;
        }else if (tableName.startsWith("dws")) {
            return MetaConst.DW_LEVEL_DWS;
        }else if (tableName.startsWith("ads")) {
            return MetaConst.DW_LEVEL_ADS;
        }else if (tableName.startsWith("dm")) {
            return MetaConst.DW_LEVEL_DM;
        }else  {
            return MetaConst.DW_LEVEL_OTHER;
        }
    }



}
